package tcln.sors.client.main;

import java.util.List;
import java.util.Map;

import tcln.sors.client.base.BaseSorsController;
import tcln.sors.client.contact.ContactModelData;
import tcln.sors.client.contact.ProductModelData;
import tcln.sors.client.core.mvc.ListModelData;
import tcln.sors.client.core.mvc.StandardControllerType;
import tcln.sors.model.contact.dom.Contact;
import tcln.sors.model.core.criteria.SortInfo;
import tcln.sors.model.core.criteria.SortOrder;
import tcln.sors.model.product.dom.Product;

/**
 * Main screen controller.
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class MainScreenController extends BaseSorsController<MainScreenModelData> {

    /**
     * {@inheritDoc}
     */
    @Override
    public String getType() {
        return StandardControllerType.MAIN_PAGE.getValue();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void initialize(Map<String, Object> requestParameters) {
        super.initialize(requestParameters);

        MainScreenModelData model = new MainScreenModelData();
        loadReferenceContacts(model);
        loadReferenceProducts(model);
        this.setModelData(model);
    }
    
    private void loadReferenceContacts(MainScreenModelData model) {
        List<Contact> contacts = getServiceFactory().getFor(Contact.class).listAll(
                SortInfo.create().add("name", SortOrder.ASC));
        ListModelData<ContactModelData> contactModels = getDataMapper().map(contacts, ContactModelData.class);
        model.setContactModelData(contactModels);
    }
    
    private void loadReferenceProducts(MainScreenModelData model) {
        List<Product> contacts = getServiceFactory().getFor(Product.class).listAll(
                SortInfo.create().add("name", SortOrder.ASC));
        ListModelData<ProductModelData> productModels = getDataMapper().map(contacts, ProductModelData.class);
        model.setProductModelData(productModels);
    }
}
